<?

class Admin_Install extends MY_Controller {

    var $install_version = '0.9.34';

    var $install_date = '05/17/2009';

    function Admin_Install() {

        parent::MY_Controller();

    }

    function index() {

        $fields = array(
            'email_address'     =>  'Email Address',
            'password'          =>  'Password',
            'passwordv'         =>  'Password Verification',
            'display_name'      =>  'Display Name',
            'site_name'         =>  'Site Name',
            'site_description'	=>  'Site Description'
        );

        $this->validation->set_fields($fields);

        $rules = array(
            'email_address'     =>  'required|valid_email',
            'password'          =>  'required|md5',
            'passwordv'         =>  'required|md5|matches[password]',
            'display_name'      =>  'required',
            'site_name'         =>  'required',
            'site_description'	=>  'required'
        );

        $this->validation->set_rules($rules);

        if ($this->validation->run()) {

            $db_array = array(
                'email_address'     =>  $this->input->post('email_address'),
                'password'          =>  $this->input->post('password'),
                'display_name'      =>  $this->input->post('display_name'),
                'user_type_id'      =>  1,
                'site_id'           =>  1,
                'date_registered'   =>  time()
            );

            $this->db->insert('inktype_users', $db_array);

            $db_array = array(
                'site_id'               =>  1,
                'site_name'             =>  $this->input->post('site_name'),
                'site_description'      =>  $this->input->post('site_description'),
                'is_default'            =>  1,
                'url_title'             =>  url_title($this->input->post('site_name')),
                'template_folder'       =>  'personified',
                'template_default_view' =>  'index'
            );

            $this->db->insert('inktype_sites', $db_array);

            $this->db->delete('inktype_version');

            $db_array = array(
                'inktype_version'   =>  $this->install_version,
                'version_date'      =>  strtotime($this->install_date)
            );

            $this->db->insert('inktype_version', $db_array);

            $this->session->set_flashdata('redirect_from_installer', TRUE);

            redirect('sessions/login');

        }

        $database_exists = FALSE;

        $admin_exists = FALSE;

        $site_exists = FALSE;

        if ($this->db->table_exists('inktype_users')) {

            $database_exists = TRUE;

            $this->db->where_in('user_type_id', '1,2,3,4');

            $query = $this->db->get('inktype_users');
            
            if ($query->num_rows()) {

                $admin_exists = TRUE;

            }

            $query = $this->db->get('inktype_sites');

            if ($query->num_rows()) {

                $site_exists = TRUE;

            }

        }

        $data = array(
            'admin_exists'      =>  $admin_exists,
            'database_exists'   =>  $database_exists,
            'site_exists'       =>  $site_exists
        );

        $this->load->view('admin/admin_install', $data);

    }

}

?>